package cn.zspt.phonebook.util;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class ChineseToPinYin {
    public static String getInit(String chinese) {
        //如果是英文字母开头
        if(chinese.charAt(0)>='a'&&chinese.charAt(0)<='z'
            ||chinese.charAt(0)>='A'&&chinese.charAt(0)<='Z'){
            return chinese.toUpperCase().charAt(0) + "";
        }

        //如果是中文开头
        if(chinese.charAt(0)>=0x4E00&&chinese.charAt(0)<=0x9FA5){
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

            String pinyin = "";
            try {
                String s = PinyinHelper.toHanYuPinyinString(chinese, format, "", false);
                pinyin += s.charAt(0);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            return pinyin;
        }

        //其它
        return "#";
    }


}
